const fs = require('fs');
const path = require('path');
const { pool } = require('../config/db');

async function initDatabase() {
  try {
    console.log('开始初始化数据库...');
    
    // 读取SQL文件
    const sqlFilePath = path.join(__dirname, '../config/database.sql');
    const sqlContent = fs.readFileSync(sqlFilePath, 'utf8');
    
    // 分割SQL语句（按分号分割，忽略注释）
    const statements = sqlContent
      .split(';')
      .map(stmt => stmt.trim())
      .filter(stmt => stmt.length > 0 && !stmt.startsWith('--'));
    
    // 执行每条SQL语句
    for (const statement of statements) {
      if (statement) {
        await pool.execute(statement);
      }
    }
    
    console.log('✓ 数据库初始化成功');
    process.exit(0);
  } catch (error) {
    console.error('✗ 数据库初始化失败:', error.message);
    process.exit(1);
  }
}

initDatabase();


